<!DOCTYPE html>
<html>
<head>
    <th:block th:include="include :: headermain('互助保障系统V3')"/>
    <title>业务办理-入会业务-参互情况</title>
    <script src="/plugin/jquery/jquery.min.js"></script>
    <script src="/plugin/jquery/jquery.plugin.js"></script>
    <script src="/plugin/vue/vue.min.js"></script>
    <script src="/plugin/layui/layui.all.js"></script>
    <script src="/unit/resource/js/script.js"></script>
</head>
<body>
<div class="page">
    <!-- 头部信息 -->
    <th:block th:include="include :: top"/>
    <div class="center">
        <div class="page-content page-wrap">
            <!-- 左侧导航 -->
            <th:block th:include="include :: left"/>
            <div class="page-wrap-container">

                <div class="page-account">
                    <div class="page-tab">
                        <a href="/hzpolicy/hzPolicyFront/policySituation">参互情况</a>
                        <a class="on" href="/hzpolicy/hzPolicyFront/policyApply">业务办理</a>
                        <a href="/hzpolicy/hzPolicyFront/policyPerson">职工入会</a>
                    </div>

                    <div class="page-tab-content" style="display:block;">
                        <div class="page-step">
                            <span class="current">选择计划</span>
                            <i class="fa fa-arrow-circle-right "></i>
                            <span>填写资料</span>
                            <i class="fa fa-arrow-circle-right "></i>
                            <span>提交审核</span>
                        </div>

                        <c:if test="${policyInfo.back == 1}">
                            <div class="page-tips error" style="margin:0 0 30px 0;">退回原因：${policyInfo.backcontent}</div>
                        </c:if>
                        <table class="page-table page-table-list policy-insurance" style=" min-width:350px ">
                            <tr>
                                <th>活动名称</th>
                                <th>参加标准</th>
                                <th>参加份数</th>
                                <th>参加人数</th>
                                <th>总金额</th>
                                <th></th>
                            </tr>
                            <c:if test="${empty policyInsurances}">
                                <tr>
                                    <td colspan="6" style="padding:30px">未选择保障活动</td>
                                </tr>
                            </c:if>
                            <c:if test="${not empty policyInsurances}">
                                <c:forEach items="${policyInsurances}" var="policyInsurance">
                                    <tr class="policy-insurance-selected-item">
                                        <td>${policyInsurance.insurancename}</td>
                                        <td>${policyInsurance.price}￥/人/份</td>
                                        <td>${policyInsurance.unitqty}份</td>
                                        <td>${policyInsurance.totalperson}人</td>
                                        <td>${policyInsurance.totalamount}</td>
                                        <td>
                                            <a class="layui-btn layui-btn-sm" href="/hzpolicy/hzPolicyFront/downloadPerson?tempName=${policyInfo.unitname}&insuranceId=${policyInsurance.id}" target="_self">下载</a>
                                            <a class="layui-btn layui-btn-sm policy-insurance-btn-person" data-id="${policyInsurance.id}">名单</a>
                                            <a class="layui-btn layui-btn-sm layui-btn-danger policy-insurance-btn-del" data-id="${policyInsurance.id}">删除</a>
                                        </td>
                                    </tr>
                                </c:forEach>
                            </c:if>

                        </table>
                        <div style="padding-top:15px;">
                            <c:if test="${empty policyInsurances}">
                                <a class="layui-btn layui-btn-normal policy-insurance-btn-add">选择计划</a>
                            </c:if>
                            <c:if test="${not empty policyInsurances}">
                                <a class="layui-btn layui-btn-normal policy-insurance-btn-add">继续添加其它计划</a>
                            </c:if>
                            <a class="layui-btn policy-insurance-btn-next">下一步</a>
                            <a class="layui-btn layui-btn-danger policy-insurance-btn-cancel" style="margin-left:15px;">取消申请</a>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
    <!-- 底部信息 -->
    <th:block th:include="include :: footer"/>
</div>

<script type="text/javascript">

    /* 添加计划 */
    $(".policy-insurance-btn-add").click(function () {
        $.soon.open({
            url: "/hzpolicy/hzPolicyFront/choiceInsurance?id=${policyInfo.id}",
            width: "80%",
            height: "95%",
            title: "选择计划",
            btn: ["确认", "取消"] ,
            success: function () {
                var data = [];
                $(".apply-upload-add-standard").each(function () {
                    var redVal = $(this).parents("tr").find("input[type='radio']:checked").val();
                    if (redVal != "0") {
                        var insuranceId = $(this).parents("tr").attr("data-id"); // 标准计划id
                        var insuranceName = $(this).parents("tr").find("#insuranceName").attr("data-id"); // 标准计划名称
                        var standardId = $(this).parents("tr").find(".apply-upload-add-standard input[type='radio']:checked").val();// 参加标准id
                        var price = $(this).parents("tr").find(".apply-upload-add-standard input[type='radio']:checked").attr("data-id"); // 参加标准id
                        var qty = $(this).parents("tr").find(".apply-upload-add-qty input[type='radio']:checked").val(); // 份数
                        data.push({
                            insuranceId: insuranceId,
                            insuranceName: insuranceName,
                            standardId: standardId,
                            price: price,
                            unitQty: qty
                        });
                    }
                });
                if (data.length == 0) {
                    layer.msg("请选择参加的保障活动");
                    return;
                }
                if($(".apply-upload-add-form input[name=file]")[0].files[0] == null){
                    layer.msg("请选择文件");
                    return;
                }
                var idx = layer.load(0);
                var form = new FormData();
                form.append("policyId", "${policyInfo.id}");
                form.append("insuranceInfo", JSON.stringify(data));
                form.append("templateFile", $(".apply-upload-add-form input[name=file]")[0].files[0]);
                $.ajax({
                    type: "post",
                    url: "/hzpolicy/hzPolicyFront/savePolicyInsurance",
                    data: form,
                    dataType: "json",
                    processData: false,
                    contentType: false,
                    success: function (data) {
                        layer.close(idx);
                        if (data.success) {
                            layer.msg(data.msg);
                            setTimeout(function () {
                                window.location.reload();
                            }, 1000);
                        } else {
                            if (!isEmpty(data.body)) {
                                var str = '<table class="page-table page-table-list page-table-error" style="padding-top:10px;">';
                                str += '	<tr>';
                                str += '		<th>序号</th>';
                                str += '		<th>姓名</th>';
                                str += '		<th>性别</th>';
                                str += '		<th>身份证</th>';
                                str += '		<th>错误详情</th>';
                                str += '	</tr>';
                                for (var i = 0; i < data.body.errorInfos.length; i++) {
                                    var item = data.body.errorInfos[i];
                                    str += '	<tr>';
                                    str += '		<td>' + item.code + '</td>';
                                    str += '		<td>' + item.name + '</td>';
                                    str += '		<td>' + item.sex + '</td>';
                                    str += '		<td>' + item.idCard + '</td>';
                                    str += '		<td>' + item.errorMsg + '</td>';
                                    str += '	</tr>';
                                }
                                str += '</table>';
                                $(".person-list").html(str);
                            }else{
                                layer.msg(data.msg);
                                if(data.msg == "请求地址更换，请刷新重试。"){
                                    setTimeout(function () {
                                        window.location.replace("/hzpolicy/hzPolicyFront/policyApply");
                                    }, 1000);
                                }
                            }
                        }
                    }, error: function () {
                        layer.close(idx);
                        layer.msg("数据提交失败");
                    }
                });

            }
        });
    });

    /**
     * 判断对象是否为空
     * @param obj
     * @returns {boolean}
     */
    function isEmpty(obj){
        for(var o in obj){
            return false;
        }
        return true;
    }

    /* 删除计划 */
    $(".policy-insurance-btn-del").click(function () {
        var id = $(this).attr("data-id");
        layer.confirm('确实要删除该计划吗？', {
            btn: ['删除', '取消']
        }, function () {
            $.soon.ajax({
                url: "/hzpolicy/hzPolicyFront/delInsurance?id=" + id,
                success: function (data) {
                    layer.msg(data.msg);
                    setTimeout(function () {
                        if (data.success) {
                            window.location.reload();
                        }else{
                            window.location.replace("/hzpolicy/hzPolicyFront/policyApply");
                        }
                    }, 1000);
                }
            });
        });


    });

    /* 取消申请 */
    $(".policy-insurance-btn-cancel").click(function () {
        layer.confirm('确实要删除本次申请吗？', {
            btn: ['删除', '取消']
        }, function () {
            $.soon.ajax({
                url: "/hzpolicy/hzPolicyFront/cancelPolery?id=${policyInfo.id}",
                success: function (data) {
                    layer.msg(data.msg);
                    setTimeout(function () {
                        window.location.href = "/hzpolicy/hzPolicyFront/policyApply";
                    }, 1000);
                }
            });
        });


    });

    /* 名单 */
    $(".policy-insurance-btn-person").click(function () {

        $.soon.open({
            url: "/hzpolicy/hzPolicyFront/personRosterData?pageNo=1&pageSize=10&id=" + $(this).attr("data-id"),
            width: "70%",
            height: "80%",
            iframe: true,
            title: "职工列表",
            end: function(){
                window.location.reload();
            }
        });

    });

    /* 下一步 */
    $(".policy-insurance-btn-next").click(function () {
        if ($(".policy-insurance-selected-item").length > 0) {
            // 判断所选计划是否都有人员参加
            $.soon.ajax({
                url: "/hzpolicy/hzPolicyFront/checkPerson?id=${policyInfo.id}",
                success: function (data) {
                    if (data.success) {
                        window.location.href = "/hzpolicy/hzPolicyFront/policyApplyConfirm?id=${policyInfo.id}";
                    }else{
                        layer.msg(data.msg);
                    }
                }
            });
        } else {
            layer.msg("请选择参加的计划");
        }
    });

</script>

</body>
</html>
